<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="75px">
      <el-form-item label="用户账号" prop="userId" v-has-role="['admin']">
        <el-select v-model="queryParams.userId" placeholder="请选择" @keyup.enter.native="handleQuery">
          <el-option
            v-for="item in userOptions"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="到账状态" prop="status">
        <el-select
          style="width: 150px"
          v-model="queryParams.status" placeholder="选择到账状态">
          <el-option label="未充值" value="0"></el-option>
          <el-option label="已到账" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="充值渠道" prop="rechargeType">
        <el-select
          style="width: 150px"
          v-model="queryParams.rechargeType" placeholder="选择充值渠道">
          <el-option label="自助充值" value="自助充值"></el-option>
          <el-option label="人工充值" value="人工充值"></el-option>
          <el-option label="其它" value="其它"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="支付单号" prop="outTradeNo">
        <el-input
          style="width: 150px"
          v-model="queryParams.outTradeNo"
          placeholder="请输入支付单号"
          clearable
          @keyup.enter.native="handleQuery"/>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="2">
        <el-button type="primary" size="mini" @click="open=true">我要充值</el-button>
      </el-col>
      <el-col :span="2">
        <el-button
          type="warning"
          plai
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:recharge:export']">导出
        </el-button>
      </el-col>
      <right-toolbar v-has-role="['admin']" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="rechargeList">
      <el-table-column label="单号" align="center" prop="id"/>
      <el-table-column label="充值渠道" align="center" prop="rechargeType">
        <template slot-scope="scope">
          <span>{{ scope.row.rechargeType }}</span>
          <span style="display: block">
            {{
              scope.row.outTradeNo
            }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="用户ID" align="center" prop="userId" v-has-role="['admin']"/>
      <el-table-column label="充值前余额" align="center" prop="beforeBalance"/>
      <el-table-column label="充值额" align="center" prop="recharge"/>
      <el-table-column label="充值后余额" align="center" prop="afterBalance"/>
      <el-table-column label="状态" align="center" prop="rechargeType">
        <template slot-scope="scope">
          <span>{{ scope.row.status == '1' ? '已到账' : '未充值' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="时间" align="center" prop="createTime"/>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"/>

    <el-dialog title="自助充值" :visible.sync="open" :destroy-on-close="true" :close-on-click-modal="false">
      <pay v-if="open"/>
    </el-dialog>
  </div>
</template>

<script>
  import pay from "@/views/components/pay.vue"
  import {listRecharge} from "@/api/system/recharge"
  import {userName} from "@/api/system/user";

  export default {
    name: "Recharge",
    components: {pay},
    data() {
      return {
        userOptions: [],
        // 遮罩层
        loading: true,
        open: false,
        // 总条数
        total: 0,
        // 充值记录表格数据
        rechargeList: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,

          rechargeType: null,
          userId: null,
          outTradeNo: null,
          status: null
        }
      };
    },
    created() {
      this.getList();
      userName().then(response => {
        this.userOptions = response.data;
      });
    },
    methods: {
      /** 查询充值记录列表 */
      getList() {
        this.loading = true;
        listRecharge(this.queryParams).then(response => {
          this.rechargeList = response.rows;
          this.total = response.total;
          this.loading = false;
        })
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('system/recharge/export', {
          ...this.queryParams
        }, `recharge_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
